package com.person.swagger.controller;

/*
 *@Description controller 测试类
 *@Author java02
 *@Date 2018/8/6 15:10
 *
 */


import com.person.swagger.vo.PersonInfo;
import com.person.swagger.vo.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "swagger2使用示例", description = "接口文档生成使用案例")
@RequestMapping(value = "/swagger2Management")
public class Swagger2Controller {

    /**
     * 无参数，无返回值接口测试
     */
    @ApiOperation(value = "无参数，无返回值接口测试")
    @PostMapping(value = "/test")
    public void test() {
        System.out.println("welcome Spring Boot to here!");
    }

    /**
     * 单参数，无返回值接口测试
     *
     * @param name
     */
    @ApiOperation(value = "单参数，无返回值接口测试")
    //@ApiImplicitParam(name ="name",value = "姓名",required = true,dataType = "String")//value  为参数的描述，required是否必填
    @PostMapping(value = "/test1")
    public void test1(@RequestBody String name) {
        System.out.println("欢迎 " + name + "归来");
    }

    /**
     * 多参数，无返回值接口测试
     *
     * @param name
     * @param age
     */
    @ApiOperation(value = "多参数，无返回值接口测试")//value  为参数的描述，required是否必填
    /*@ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "age", value = "年龄", paramType = "query",required = true, dataType = "Integer")
    })*/
    @PostMapping(value = "/test2")
    public void test2(@RequestParam String name, @RequestParam String age) {
        System.out.println("欢迎 " + name + "归来,他今年" + age + "岁");
    }

    /**
     * 单对象，无返回值接口测试
     *
     * @param user
     */
    @ApiOperation(value = "单对象，无返回值接口测试")
    @PostMapping(value = "/test3")
    public void test3(@RequestBody UserInfo user) {
        System.out.println("用户：" + user.getUsername() + "   密码：" + user.getPassword());
    }

    /**
     * @param userInfo
     * @param personInfo
     * @date 2018-8-9
     * @Auth wangpeng
     * @Description 多对象传递
     */
    @ApiOperation(value = "多对象，无返回值接口测试")
    @PostMapping(value = "/test4")
    public void test4(@ModelAttribute UserInfo userInfo, @ModelAttribute PersonInfo personInfo) {
        System.out.println(userInfo.getUsername());
    }

    @ApiOperation("单参数，有返回值接口测试")
    //利用ModelAndView 返回 ，  ModelAndView 构造函数中写的就是视图的名称，SpringBoot 默认从 resources /templates下面招
    //@ApiImplicitParam(name ="name",value = "姓名",required = true,dataType = "String",paramType = "query")//value  为参数的描述，required是否必填
    @PostMapping(value = "/test5")
    public ModelAndView test5(@RequestParam String name) {
        //setting   viewName    构造函数可以直接设置 viewName
        ModelAndView modelAndView = new ModelAndView("success");
        modelAndView.addObject("name", name);
        return modelAndView;
    }

    @ApiOperation("多参数，有返回值接口测试")
    //利用ModelAndView 返回 ，  ModelAndView 构造函数中写的就是视图的名称，SpringBoot 默认从 resources /templates下面照
    /*@ApiImplicitParams({ //int类型只能专递integer   注意i不能大写，否则会报错
             @ApiImplicitParam(name ="name",value = "姓名",required = true,dataType = "string",paramType = "query"),
             @ApiImplicitParam(name ="age",value = "年龄",required = true,dataType = "integer",paramType = "query")
    })*/
    @PostMapping(value = "/test6")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "age", value = "年龄", required = true, dataType = "integer")
    })
    public ModelAndView test6(@RequestParam String name, @RequestParam Integer age, HttpSession session) {
        //setting   viewName    构造函数可以直接设置 viewName
        ModelAndView modelAndView = new ModelAndView("success2");
        Map <String, Object> map = new HashMap <String, Object>();
        map.put("name", name);
        map.put("age", age);
        modelAndView.addAllObjects(map);
        return modelAndView;
    }

    /**
     * @param userInfo
     * @return
     * @desc 在swagger2测试中如果用了@RequestBody注解 ，实际上就没有必要在使用 @ApiImplicitParam注解，因为这两个注解会被认为是传递两个对象
     */
    @ApiOperation(value = "单对象，有返回值接口测试")
    //利用ModelAndView 返回 ，  ModelAndView 构造函数中写的就是视图的名称，SpringBoot 默认从 resources /templates下面招
    //@ApiImplicitParam(name ="userInfo",value = "用户表",required = true,dataTypeClass =UserInfo.class, paramType = "body")//value  为参数的描述，required是否必填
    @PostMapping(value = "/test7")
    public ModelAndView test7(@RequestBody UserInfo userInfo) {
        //setting   viewName    构造函数可以直接设置 viewName
        ModelAndView modelAndView = new ModelAndView("success3");
        modelAndView.addObject(userInfo);
        return modelAndView;
    }


    @ApiOperation("多对象，有返回值接口测试")
    @PostMapping(value = "/test8")
    public ModelAndView test8(@ModelAttribute UserInfo userInfo, @ModelAttribute PersonInfo personInfo) {
        ModelAndView mv = new ModelAndView("success4");
        mv.addObject(userInfo);
        mv.addObject(personInfo);
        return mv;
    }

    @ApiOperation("map传值")
    @RequestMapping(value = "/test9", method = RequestMethod.POST)
    public void test9(@RequestBody Map<String, Object> map) {
        System.out.println(map.get("name"));
    }
}